package com.alibaba.product.service.impl;

import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.uitl.BaiduAuditUtils;
import com.alibaba.product.domain.Product;
import com.alibaba.product.domain.ProductDetail;
import com.alibaba.product.domain.ProductOnlineAuditLog;
import com.alibaba.product.mapper.ProductDetailMapper;
import com.alibaba.product.mapper.ProductMapper;
import com.alibaba.product.mapper.ProductOnlineAuditLogMapper;
import com.alibaba.product.service.IProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gp
 * @since 2023-03-17
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {


    @Autowired
    private ProductDetailMapper detailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper logMapper;

    @Override
    public void add(Product product) {
        product.setCreatetime(new Date());
        product.setState(0);
        product.setSalecount(0L);
//        UserInfo userInfo = LoginContext.getUserInfo();
//        Long id = userInfo.getShop().getId();
//        product.setShopId(id);
        super.add(product);
        // 保存详情
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        detailMapper.add(detail);
    }

    @Override
    public void update(Product product) {
        // 更新food
        super.update(product);
        // 更新detail
        detailMapper.update(product.getDetail());
    }


    @Override
    public void onsale(List<Long> ids) {
        // 参数校验
        // 判断是否是上架 若果是上架 就不管 我们只管下架状态的
        List<Product> products =  productMapper.findByIds(ids);
        if (CollectionUtils.isEmpty(products)){
            return;
        }
        // 百度AI审核
        List<ProductOnlineAuditLog> logList = new ArrayList<>();
        List<Long> productIds = new ArrayList<>();
        products.forEach(product->{
            StringBuilder sb = new StringBuilder();
            // 名称
            if (!BaiduAuditUtils.TextCensor(product.getName())){
                sb.append("商品名称审核不通过！食物id为").append(product.getId()).append(";");
            }
            // 图片
            String resources = product.getResources();
            if (StringUtils.isNotBlank(resources)){
                String[] split = resources.split(",");
                for (int i = 0; i < split.length; i++) {
                    if (!BaiduAuditUtils.ImgCensor(split[i])){
                        sb.append("商品图片审核不通过！食物id为").append(product.getId());
                    }
                }
            }
            // 去添加日志
            if (sb.length()>0){
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setAuditId(-1L);
                log.setNote(sb.toString());
                log.setAuditTime(new Date());
                log.setProductId(product.getId());
                log.setState(0);
                logList.add(log);
                return;
            }
            productIds.add(product.getId());
        });
        // 修改状态
        if (!CollectionUtils.isEmpty(productIds)){
            // 批量修改食物上架
            productMapper.batchUpdate(productIds);
        }
        // 添加日志--只有审核不通过才添加
        if (!CollectionUtils.isEmpty(logList)){
            // 批量添加
            logMapper.batchAdd(logList);
        }

    }

    @Override
    public void offsale(List<Long> ids) {
        // 查询下架的食物
        List<Product> products =  productMapper.findOffsaleByIds(ids);
        if (CollectionUtils.isEmpty(products)){
            return;
        }
        StringBuilder sb = new StringBuilder();
        List<ProductOnlineAuditLog> logList = new ArrayList<>();
        List<Long> productIds = new ArrayList<>();
        products.stream().forEach(product -> {
            sb.append("食物下架成功！食物id为").append(product.getId()).append(";");
            // 去添加日志
            if (sb.length()>0){
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setAuditId(-1L);
                log.setNote(sb.toString());
                log.setAuditTime(new Date());
                log.setProductId(product.getId());
                log.setState(1);
                logList.add(log);
            }
            productIds.add(product.getId());
        });

        // 修改状态
        if (!CollectionUtils.isEmpty(productIds)){
            // 批量修改食物下架
            productMapper.batchUpdateOffsale(productIds);
        }
        // 添加日志--只有审核不通过才添加
        if (!CollectionUtils.isEmpty(logList)){
            // 批量添加
            logMapper.batchAdd(logList);
        }
    }

}
